package org.jeecg.modules.simulator.model.train.event;

import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.simulator.model.LineHelper;
import org.jeecg.modules.simulator.model.MetroTrain;
import org.jeecg.modules.simulator.model.OffTrainNumRange;
import org.jeecg.modules.simulator.model.line.Line;
import org.jeecg.modules.simulator.model.train.event.msg.Train2LastStationMsg;
import org.jeecg.modules.simulator.rooter.LineRooter;
import org.jeecg.modules.simulator.model.line.park.Parker;
import org.jeecg.modules.simulator.model.line.station.station.Station;
import org.jeecg.modules.simulator.model.line.station.station.TransferStation;
import org.jeecg.modules.simulator.model.line.station.strategy.ClearOffTrainStrategy;
import org.jeecg.modules.simulator.model.line.station.strategy.ITrainStrategy;
import org.jeecg.modules.simulator.model.line.station.strategy.NormalOffTrainStrategy;
import org.jeecg.modules.simulator.model.train.action.TrainActionRecorders;
import org.jeecg.modules.simulator.model.train.event.msg.SimpleActionRecorderMsg;
import org.jeecg.modules.simulator.model.train.event.msg.Train2StationMsg;
import lombok.Getter;
import lombok.Setter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: gp2
 * @description:
 * @author: renzhg
 * @create: 2024-09-07 13:45
 **/
@Component
@Slf4j
public class TrainListener {
    private static final Logger logger = LoggerFactory.getLogger(TrainListener.class);
    @Setter
    @Getter
    Map<String, TransferStation> transferNameStationMap = new HashMap<>();

    //    @Setter
//    @Getter
//    Map<String, TransferStation> transferStationMap = new HashMap<>();
    //总station
    @Setter
    @Getter
    Map<String, Station> stationMap = new HashMap<>();//key为id
    @Setter
    @Getter
    Map<String, Parker> parkersMap = new HashMap<>();

    @Setter
    @Getter
    Map<String, Station> stationNameMap = new HashMap<>();
    @Setter
    @Getter
    Map<String, LineRooter> lineRooterMap = new HashMap<>();

    @Setter
    @Getter
    Map<String, Line> lineMap = new HashMap<>();

    @Setter
    @Getter
    Map<String, OffTrainNumRange> offTrainNumRangeMap;
//    @Setter
//    @Getter
//    private OrbitPath upOrbitPath;//上行轨道
//    @Setter
//    @Getter
//    private OrbitPath downOrbitPath;//下行轨道


    @EventListener
    public void startTrain(TrainUpEvent event) {

        List<TrainActionRecorders> trainActionRecordersList = (List<TrainActionRecorders>) event.getSource();
        if (trainActionRecordersList.isEmpty()) {
            return;
        }
        trainActionRecordersList.forEach(o -> {
            String firstName = o.getFristTrainActionRecorder().getName();
            for (Map.Entry<String, Parker> entry : parkersMap.entrySet()) {
                if (entry.getValue().getUpOut().getName().equals(firstName) || entry.getValue().getDownOut().getName().equals(firstName)) {
                    entry.getValue().saveTrainRecorder(o);
                }
            }
        });
    }

    //    Map<String, Station> stationNameMap = new HashMap<>();
    @EventListener
    public void stopTrain(TrainStopEvent event) {

        Train2StationMsg  train2StationMsg=(Train2StationMsg)event.getSource();
        String key=train2StationMsg.getStopStationName()+"_"+train2StationMsg.getBelongLineId();
        Station station=stationNameMap.get(key);
        MetroTrain metroTrain=train2StationMsg.getTrain();
//        logger.debug("第"+metroTrain.getTrainId()+"_"+metroTrain.getTrainNum()+"次车，"+"当前停靠站："+train2StationMsg.getStopStationName());
//        System.out.println("第"+metroTrain.getTrainId()+"_"+metroTrain.getTrainNum()+"次车，"+"当前停靠站："+train2StationMsg.getStopStationName());
        ITrainStrategy iTrainStrategy;
        if(train2StationMsg.isLast()){
            iTrainStrategy=new ClearOffTrainStrategy();
        }else{
            iTrainStrategy= new NormalOffTrainStrategy(lineMap,transferNameStationMap);
        }
        boolean ok =  LineHelper.doExchange(station,metroTrain,
                train2StationMsg.getTime(),
                offTrainNumRangeMap,stationNameMap,
                iTrainStrategy);

    }








    @EventListener
    public void parkSendTrain(  ParkSendTrainEvent event){
        MetroTrain metroTrain = (MetroTrain) event.getSource();
        lineRooterMap.forEach((k,v)->{
            if(v.getLineId().equals(metroTrain.getBelongLineId())){
                v.getTrainManager().addTrain2trainMap(metroTrain);
            }
        });
    }

    @EventListener
    public void downLoadTran(TrainDownLoadEvent event) {
        Train2LastStationMsg train2LastStationMsg = (Train2LastStationMsg) event.getSource();
//        Station station = stationNameMap.get(train2LastStationMsg.getStopStationName());

        parkersMap.forEach((k, v) -> {
            if ( v.getDownOut().getName().equals(train2LastStationMsg.getStopStationName())) {

                v.downLoadTran(train2LastStationMsg.getTrain());
            }
        });






//        lineRooterMap.forEach((k,v)->{
//v.getLine().getUpOrbitPath().getFirstPointName();
//        });
//        for (Map.Entry<String, Parker> entry : parkersMap.entrySet()) {
//            entry.getValue().downLoadTrain(metroTrain);
//        }
    }


    @EventListener
    public void insertRecords2TrainManager(SimpleActionRecorderEvent event){
        SimpleActionRecorderMsg simpleActionRecorderMsg=  (SimpleActionRecorderMsg) event.getSource();
        lineRooterMap.forEach((k,v)->{
            if(v.getLineId().equals(simpleActionRecorderMsg.getLineId())){
                v.getTrainManager().inssertTrainActionRDsMap(simpleActionRecorderMsg.getKey(),simpleActionRecorderMsg.getTrainActionRecorders());
                v.getTrainManager().getTrainActionRdsManager().getTrainActionRecordersMap().put(simpleActionRecorderMsg.getKey(),simpleActionRecorderMsg.getTrainActionRecorders());
//
            }
        });

    }
}
